package com.rabbit.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rabbit.R;

/**
 * LocalZoomView
 * 
 * @author zhilei.liu 2014-12-9下午5:37:02
 */
public class LocalZoomView extends RelativeLayout {

    private ImageView mImageView;
    private TextView mCenterTextView, mDownTextView;
    private View mLine;
    private Drawable mDrawable;
    private String mCenterText, mDownText;
    private Animation mZoonAnimation, mLargeAnimation;
    private static final float ZOOM_SCALE = 0.95f;

    public LocalZoomView(Context context) {
        super(context);
        initView();
    }

    public LocalZoomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        parseAttrs(attrs);
    }

    private void initView() {

        ((LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE)).inflate(
                R.layout.view_local_zoom, this, true);

        setLongClickable(true);
        mImageView = (ImageView) findViewById(R.id.iv_local_zoom);
        mCenterTextView = (TextView) findViewById(R.id.tv_local_zoom_center);
        mDownTextView = (TextView) findViewById(R.id.tv_local_zoom_down);
        mLine = findViewById(R.id.line_local_zoom);

        mZoonAnimation = new ScaleAnimation(1f, ZOOM_SCALE, 1f, ZOOM_SCALE,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mZoonAnimation.setDuration(300);
        mZoonAnimation.setFillAfter(true);
        mLargeAnimation = new ScaleAnimation(ZOOM_SCALE, 1f, ZOOM_SCALE, 1f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mLargeAnimation.setDuration(100);
        mLargeAnimation.setFillAfter(true);
        // mZoonAnimation = new ScaleAnimation(getLeft(), getLeft()+3,
        // getBottom(), getBottom())
    }

    private void parseAttrs(AttributeSet attrs) {

        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.LocalZoom);
        mDrawable = a.getDrawable(R.styleable.LocalZoom_upDrawable);

        mCenterText = a.getString(R.styleable.LocalZoom_centerText);
        mDownText = a.getString(R.styleable.LocalZoom_downText);

        a.recycle();

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mCenterTextView.setText(mCenterText);
        mDownTextView.setText(mDownText);
        mImageView.setImageDrawable(mDrawable);
    }

    public void setLineVisibilty(int visibility) {
        mLine.setVisibility(visibility);
    }

    public void setDownTextVisibilty(int visibility) {
        mDownTextView.setVisibility(visibility);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            showZoom();
            setBackgroundColor(getResources().getColor(R.color.zoom_view_white));
            break;
        case MotionEvent.ACTION_UP:
            showLarge();
            setBackgroundColor(getResources().getColor(R.color.zoom_view_bg));
            break;
        }
        invalidate();
        return super.onTouchEvent(event);
    }

    public void showZoom() {
        setAnimation(mZoonAnimation);
        mZoonAnimation.start();
    }

    public void showLarge() {
        setAnimation(mLargeAnimation);
        mLargeAnimation.start();
    }

}
